﻿Imports System.Data
Imports Microsoft.VisualBasic
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.Profile
Imports System.Web.Security
Imports Equipment.Business
Imports Equipment.Core
Imports Equipment.DataAccess

Public Class UserManager

#Region "Functions"
    Public Shared Function GetAllFor(ByVal forUser As IUserInfo) As IEnumerable(Of User)
        If forUser.IsSuperUser Then
            Return User.RetrieveAllByStatus(UserStatus.Active)
        Else
            Return User.RetrieveAll(forUser.SiteID, UserStatus.Active)
        End If
    End Function

    Public Shared Function RetrieveAll() As DataTable
        Dim dt As New DataTable()

        dt.Columns.Add(New DataColumn("UserName", GetType([String])))
        dt.Columns.Add(New DataColumn("UserFullName", GetType([String])))
        dt.Columns.Add(New DataColumn("Email", GetType([String])))
        dt.Columns.Add(New DataColumn("PhoneNumber", GetType([String])))

        Dim profileColl As ProfileInfoCollection = ProfileManager.GetAllProfiles(ProfileAuthenticationOption.Authenticated)

        For Each pi As ProfileInfo In profileColl
            Dim userProfile As UserProfile = DirectCast(ProfileBase.Create(pi.UserName), UserProfile)
            Dim dr As DataRow = dt.NewRow()

            dr("UserName") = userProfile.UserName
            dr("UserFullName") = userProfile.FullName
            dr("Email") = userProfile.EmailAddress
            dr("PhoneNumber") = userProfile.PhoneNumber
            dt.Rows.Add(dr)
        Next

        Return dt
    End Function

    Public Shared Sub Delete(ByVal username As String)

        Dim profileColl As ProfileInfoCollection = ProfileManager.FindProfilesByUserName(ProfileAuthenticationOption.Authenticated, username)
        ProfileManager.DeleteProfiles(profileColl)

        Membership.DeleteUser(username)

    End Sub
#End Region

End Class
